package org.hgvc.webapp.ets.jsf.beans.agent;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberComp;
import org.hgvc.webapp.ets.bo.MemberInd;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.MemberConstant;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;


public class AgentProspectMagmtBean extends AbstractBean {

	private String name;
	private String regno;
	private MemberService service;
	private Member member;
	private MemberInd memberInd;
	private MemberComp memberComp;
	private List<Member> memberList;
	private String agreeAckStatus = MemberConstant.MEMBER_STATUS_PROSPECT_ACK_AGREEMENT;
	private String checkListPedingStatus = MemberConstant.MEMBER_STATUS_PROSPECT_PENDING_CHECK_LIST;
	private String pendingMemberStatus = MemberConstant.MEMBER_STATUS_PENDING_MEMBER;
	public static final String BEAN_NAME="agentProspectMagmtBean";
	public static final String NAV_LIST="agentProspectMgmt";
	public Agent agent;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getRegno() {
		return regno;
	}
	public void setRegno(String regno) {
		this.regno = regno;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public MemberInd getMemberInd() {
		return memberInd;
	}
	public void setMemberInd(MemberInd memberInd) {
		this.memberInd = memberInd;
	}
	public MemberComp getMemberComp() {
		return memberComp;
	}
	public void setMemberComp(MemberComp memberComp) {
		this.memberComp = memberComp;
	}
	public String getCheckListPedingStatus() {
		return checkListPedingStatus;
	}
	public void setCheckListPedingStatus(String checkListPedingStatus) {
		this.checkListPedingStatus = checkListPedingStatus;
	}
	public String getAgreeAckStatus() {
		return agreeAckStatus;
	}
	public String getPendingMemberStatus() {
		return pendingMemberStatus;
	}
	public void setService(MemberService service) {
		this.service = service;
	}
	
	public Agent getAgent() {
		return agent;
	}
	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	/*---------------------------------------------------------------------*/
	public List<Member> getMemberList() {
		try{
			if(agent ==null){
				AgentBean bean = (AgentBean)FacesUtils.getManagedBean(AgentBean.BEAN_NAME);
				agent = bean.getAgent();
			}
			memberList = service.getProspectMemberList(agent);
			memberList = service.searchRegNoPartnerNameInList(regno, memberList);
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return memberList;
	}
	
	public String searchAction(){
		return NAV_LIST;
	}
	
	public String backAction(){
		return AgentBean.NAV_PORTAL;
	}
	
	public String navListAction(){
		return NAV_LIST;
	}
	
	public void checkListActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("vmemberId");
		String id =(String)param.getValue();
		for(Member member:memberList){
			if(member.getMemberId().equalsIgnoreCase(id)){
				this.member = member;
				break;
			}
		}
	}
	
	public String checkListAction(){
		AgentProspectDocCheckListBean bean = (AgentProspectDocCheckListBean)FacesUtils.getManagedBean(AgentProspectDocCheckListBean.BEAN_NAME);
		return bean.navCheckListAction(this.member);
	}
	
	public void convertActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("amemberId");
		String id =(String)param.getValue();
		for(Member member:memberList){
			if(member.getMemberId().equalsIgnoreCase(id)){
				this.member = member;
				break;
			}
		}
	}
	
	public String convertAction(){
		AgentMemberConversionBean bean = (AgentMemberConversionBean)FacesUtils.getManagedBean(AgentMemberConversionBean.BEAN_NAME);
		return bean.navMemberConversionAction(this.member);
	}
	
	public void editProspectActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("cmemberId");
		String id =(String)param.getValue();
		for(Member member:memberList){
			if(member.getMemberId().equalsIgnoreCase(id)){
				this.member = member;
				break;
			}
		}
	}
	
	public String editProspectAction(){
		if(member.getMemberType().equalsIgnoreCase(CommonConstant.MEMBER_TYPE_COMP)){
			AgentMemberCompProfileBean bean = (AgentMemberCompProfileBean)FacesUtils.getManagedBean(AgentMemberCompProfileBean.BEAN_NAME);
			return bean.navViewAction(member);
		}else{
			AgentMemberIndProfileBean bean = (AgentMemberIndProfileBean)FacesUtils.getManagedBean(AgentMemberIndProfileBean.BEAN_NAME);
			
			return bean.navViewAction(member);
		}
	}
}
